package me.zhengjie.org.employeechange.service.impl;

import me.zhengjie.org.employeechange.domain.EmployeeChange;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.org.employeechange.repository.EmployeeChangeRepository;
import me.zhengjie.org.employeechange.service.EmployeeChangeService;
import me.zhengjie.org.employeechange.service.dto.EmployeeChangeDto;
import me.zhengjie.org.employeechange.service.dto.EmployeeChangeQueryCriteria;
import me.zhengjie.org.employeechange.service.mapper.EmployeeChangeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Service
//@CacheConfig(cacheNames = "employeeChange")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmployeeChangeServiceImpl implements EmployeeChangeService {

    private final EmployeeChangeRepository employeeChangeRepository;

    private final EmployeeChangeMapper employeeChangeMapper;

    public EmployeeChangeServiceImpl(EmployeeChangeRepository employeeChangeRepository, EmployeeChangeMapper employeeChangeMapper) {
        this.employeeChangeRepository = employeeChangeRepository;
        this.employeeChangeMapper = employeeChangeMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(EmployeeChangeQueryCriteria criteria, Pageable pageable){
        Page<EmployeeChange> page = employeeChangeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(employeeChangeMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<EmployeeChangeDto> queryAll(EmployeeChangeQueryCriteria criteria){
        return employeeChangeMapper.toDto(employeeChangeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public EmployeeChangeDto findById(Integer id) {
        EmployeeChange employeeChange = employeeChangeRepository.findById(id).orElseGet(EmployeeChange::new);
        ValidationUtil.isNull(employeeChange.getId(),"EmployeeChange","id",id);
        return employeeChangeMapper.toDto(employeeChange);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public EmployeeChangeDto create(EmployeeChange resources) {
        return employeeChangeMapper.toDto(employeeChangeRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(EmployeeChange resources) {
        EmployeeChange employeeChange = employeeChangeRepository.findById(resources.getId()).orElseGet(EmployeeChange::new);
        ValidationUtil.isNull( employeeChange.getId(),"EmployeeChange","id",resources.getId());
        employeeChange.copy(resources);
        employeeChangeRepository.save(employeeChange);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            employeeChangeRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EmployeeChangeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EmployeeChangeDto employeeChange : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("员工", employeeChange.getEmpId());
            map.put("原中心", employeeChange.getFromCenterId());
            map.put("新中心", employeeChange.getToCenterId());
            map.put("变动时间", employeeChange.getChangeTime());
            map.put("变动说明", employeeChange.getDescribe());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}